import { v4 as uuidv4 } from 'uuid'

const shapeMove = function() {
	this.selectObject = null;
	this.selectType = null;
	this.moveType = null;
	this.beforeX = null;
	this.beforeY = null;
	this.orign = null;
	this.isMove = false;
	this.isStretch = false;
	this.selectedObject = function(obj, event, type) {
		this.selectObject = obj;
		this.selectType = type;
		this.moveType = 'move';
		let currentX = event.clientX;
		let currentY = event.clientY;
		this.beforeX = currentX;
		this.beforeY = currentY;
		this.isMove = true;
		event.stopPropagation();
		event.preventDefault();
	}
	this.startMoveObject = function(e, orign, item) {
		let currentX = e.clientX;
		let currentY = e.clientY;
		this.beforeX = currentX;
		this.beforeY = currentY;
		this.moveType = 'stretch';
		this.orign = orign;
		this.selectObject = item;
		this.selectType = item.type;
		event.stopPropagation();
		event.preventDefault();
	}
	this.moveOrStrachObject = function(e) {
		if (this.beforeX == null || this.beforeY == null || this.selectObject == null) return;
		let currentX = e.clientX;
		let currentY = e.clientY;
		let subX = currentX - this.beforeX;
		let subY = currentY - this.beforeY;
		this.beforeX = currentX;
		this.beforeY = currentY;
		// 这里要调用其他的数据信息了
		this.dealMoveStrachObj(subX, subY);
	}
	this.endMoveOrStrachObject = function(e) {
		if (this.beforeX == null || this.beforeY == null || this.selectObject == null) return;
		let currentX = e.clientX;
		let currentY = e.clientY;
		let subX = currentX - this.beforeX;
		let subY = currentY - this.beforeY;
		// 这里处理调用其他的数据信息了
		this.dealMoveStrachObj(subX, subY);
		this.beforeX = null;
		this.beforeY = null;
		this.selectObject = null;
	}
	this.initDrawLine = function(currentX, currentY, shapes, selectedDrawShape, e) {
		if (selectedDrawShape == 'line' || selectedDrawShape == 'lineArrow') {
			let line = {
					id: uuidv4().replaceAll(/-/g, ''),
					type: selectedDrawShape,
					x: currentX,
					y: currentY,
					x1: currentX,
					y1: currentY,
					isOpr: true
				}
			shapes.push(line);
			this.startMoveObject(e, 'end', line);
			return line;
		} else if (selectedDrawShape == 'rect') {
			let rect = {
				id: uuidv4().replaceAll(/-/g, ''),
				type: selectedDrawShape,
				x: currentX,
				y: currentY,
				width: 0,
				height: 0,
				isOpr: true
			}
			shapes.push(rect);
			this.startMoveObject(e, 'rb', rect);
			return rect;
		} else if (selectedDrawShape == 'prismatics') {
			let prismatics = {
				id: uuidv4().replaceAll(/-/g, ''),
				type: selectedDrawShape,
				x: currentX,
				y: currentY,
				width: 0,
				height: 0,
				path: '',
				text: '',
				isOpr: true
			}
			shapes.push(prismatics);
			this.startMoveObject(e, 'rb', prismatics);
			return prismatics;
		} else if (selectedDrawShape == 'ellipse') {
			let ellipse = {
				id: uuidv4().replaceAll(/-/g, ''),
				type: selectedDrawShape,
				x: currentX,
				y: currentY,
				width: 0,
				height: 0,
				text: '',
				cx: 0,
				cy: 0,
				rx: 0,
				ry: 0,
				isOpr: true
			}
			shapes.push(ellipse);
			this.startMoveObject(e, 'rb', ellipse);
			return ellipse;
		} else if (selectedDrawShape == 'text') {
			let text = {
				id: uuidv4().replaceAll(/-/g, ''),
				type: selectedDrawShape,
				x: currentX,
				y: currentY,
				text: '',
				isInput: true
			}
			shapes.push(text);
			return text;
		} else if (selectedDrawShape == 'triangle') {
			let path = "M" + currentX + " " + currentY;
			path = path + " L" + (currentX - 20) + " " + (currentY + 20);
			path = path + " L" + (currentX + 20) + " " + (currentY + 20);
			path = path + " L" + currentX + " " + currentY;
			let triangle = {
				id: uuidv4().replaceAll(/-/g, ''),
				type: selectedDrawShape,
				x1: currentX,
				y1: currentY,
				x2: currentX - 20,
				y2: currentY + 20,
				x3: currentX + 20,
				y3: currentY + 20,
				path: path,
				text: '',
				isInput: false,
				isOpr: true
			}
			shapes.push(triangle);
			return triangle;
		} else if (selectedDrawShape == 'pen') {
			let pen = {
				id: uuidv4().replaceAll(/-/g, ''),
				coordinates: [],
				path: '',
				type: selectedDrawShape,
			}
			pen.coordinates.push(currentX);
			pen.coordinates.push(currentY);
			pen.path = "M" + currentX + " " + currentY
			shapes.push(pen);
			this.startMoveObject(e, '', pen);
			return pen;
		}
	}
	this.dealMoveStrachObj = function(subX, subY) {
		let path = "";
		switch(this.selectType) {
			case 'function':
				this.selectObject.format.x = this.selectObject.format.x + subX;
				this.selectObject.format.y = this.selectObject.format.y + subY;
				break;
			case 'line':
			case 'lineArrow':
				if (this.moveType == 'move') {
					this.selectObject.x = this.selectObject.x + subX;
					this.selectObject.y = this.selectObject.y + subY;
					this.selectObject.x1 = this.selectObject.x1 + subX;
					this.selectObject.y1 = this.selectObject.y1 + subY;
				} else {
					if (this.orign == 'start') {
						this.selectObject.x = this.selectObject.x + subX;
						this.selectObject.y = this.selectObject.y + subY;
					} else {
						this.selectObject.x1 = this.selectObject.x1 + subX;
						this.selectObject.y1 = this.selectObject.y1 + subY;
					}
				}
				break;
			case 'text':
				this.selectObject.x = this.selectObject.x + subX;
				this.selectObject.y = this.selectObject.y + subY;
				break;
			case 'rect':
				this.rectCalculate(subX, subY);
				break;
			case 'triangle':
				if (this.moveType == 'move') {
					this.selectObject.x1 = this.selectObject.x1 + subX;
					this.selectObject.x2 = this.selectObject.x2 + subX;
					this.selectObject.x3 = this.selectObject.x3 + subX;
					this.selectObject.y1 = this.selectObject.y1 + subY;
					this.selectObject.y2 = this.selectObject.y2 + subY;
					this.selectObject.y3 = this.selectObject.y3 + subY;
					
				} else {
					if (this.orign == 'f') {
						this.selectObject.x1 = this.selectObject.x1 + subX;
						this.selectObject.y1 = this.selectObject.y1 + subY;
					} else if(this.orign == 's') {
						this.selectObject.x2 = this.selectObject.x2 + subX;
						this.selectObject.y2 = this.selectObject.y2 + subY;
					} else {
						this.selectObject.x3 = this.selectObject.x3 + subX;
						this.selectObject.y3 = this.selectObject.y3 + subY;
					}
				}
				path = "M" + this.selectObject.x1 + " " + this.selectObject.y1;
				path = path + " L" + this.selectObject.x2 + " " + this.selectObject.y2;
				path = path + " L" + this.selectObject.x3 + " " + this.selectObject.y3;
				path = path + " L" + this.selectObject.x1 + " " + this.selectObject.y1;
				this.selectObject.path = path;
				break;
			case 'prismatics':
				this.rectCalculate(subX, subY);
				// 这里进行计算各个顶点
				path = "M" + this.selectObject.x + " " + (this.selectObject.y + this.selectObject.height / 2);
				path = path + " L" + (this.selectObject.x + this.selectObject.width / 2) + " " + this.selectObject.y;
				path = path + " L" + (this.selectObject.x + this.selectObject.width) + " " + (this.selectObject.y + this.selectObject.height / 2);
				path = path + " L" + (this.selectObject.x + this.selectObject.width / 2) + " " + (this.selectObject.y + this.selectObject.height);
				path = path + " L" + this.selectObject.x + " " + (this.selectObject.y + this.selectObject.height / 2);
				this.selectObject.path = path;
				break;
			case 'circle':
				break;
			case 'ellipse':
				this.rectCalculate(subX, subY);
				this.selectObject.cx = this.selectObject.x + this.selectObject.width / 2;
				this.selectObject.cy = this.selectObject.y + this.selectObject.height / 2;
				this.selectObject.rx = this.selectObject.width / 2;
				this.selectObject.ry = this.selectObject.height / 2;
				break;
			case 'rhomboid':
				break;
			case 'pen':
				if (this.moveType == 'move') {
					path = "";
					for(let i = 0; i < this.selectObject.coordinates.length; i += 2) {
						this.selectObject.coordinates[i] = this.selectObject.coordinates[i] + subX;
						this.selectObject.coordinates[i + 1] = this.selectObject.coordinates[i + 1] + subY;
						if (i == 0) {
							path = "M" + this.selectObject.coordinates[i] + " " + this.selectObject.coordinates[i + 1];
						} else {
							path = path + " L" + this.selectObject.coordinates[i] + " " + this.selectObject.coordinates[i + 1];
						}
					}
					this.selectObject.path = path;
				} else {
					let len = this.selectObject.coordinates.length;
					let x = this.selectObject.coordinates[len - 2] + subX;
					let y = this.selectObject.coordinates[len - 1] + subY;
					this.selectObject.coordinates.push(x);
					this.selectObject.coordinates.push(y);
					path = this.selectObject.path + " L" + x + " " + y;
					this.selectObject.path = path;
				}
				break;
		}
	}
	this.rectCalculate = function(subX, subY) {
		if (this.moveType == 'move') {
			this.selectObject.x = this.selectObject.x + subX;
			this.selectObject.y = this.selectObject.y + subY;
		} else {
			if (this.orign == 'lt') {
				let w = this.selectObject.width - subX;
				let h = this.selectObject.height - subY;
				if (w < 0 || h < 0) return;
				this.selectObject.x = this.selectObject.x + subX;
				this.selectObject.y = this.selectObject.y + subY;
				this.selectObject.width = w;
				this.selectObject.height = h;
			} else if(this.orign == 'lb') {
				let h = this.selectObject.height + subY;
				let w = this.selectObject.width - subX;
				if (w < 0 || h < 0) return;
				this.selectObject.x = this.selectObject.x + subX;
				this.selectObject.height = h;
				this.selectObject.width = w;
			} else if (this.orign == 'rt') {
				let w = this.selectObject.width + subX;
				let h = this.selectObject.height - subY;
				if (w < 0 || h < 0) return;
				this.selectObject.y = this.selectObject.y + subY;
				this.selectObject.width = w;
				this.selectObject.height = h;
			} else {
				let w = this.selectObject.width + subX;
				let h = this.selectObject.height + subY;
				if (w < 0 || h < 0) return;
				this.selectObject.width = w;
				this.selectObject.height = h;
			}
		}
	}
}

export default shapeMove;